(function(D){
    var addRelation, autocompleteRelations, domInput, modalWindow;

    D.relations = {};

    addRelation = function(item){
        var vocabularyLabel, relationLabel, split, vocabularyId=false, 
            makeRelation, encodedVocabularyLabel, encodedRelationLabel, timestamp;

        makeRelation = function(){
            timestamp += 1;
            D.request("addRelation.txt",
                {
                    vocabId: vocabularyId,
                    relationLabel: encodedRelationLabel,
                    user: D.userName,
                    timestamp: timestamp
                }, null, true);
            D.relations[item]={
                id: 'http://desanti.org/relations/'+timestamp,
                label: relationLabel,
                vocabularyId: vocabularyId,
                vocabularyLabel: vocabularyLabel
            };
            autocompleteRelations();
        }
        if(!confirm("ajouter la relation " + item + " ?")) return;
        split = item.split(":");
        if(split.length != 2){alert("relation mal nommée"); return;}
        vocabularyLabel = split[0].toLowerCase();
        relationLabel = split[1].toLowerCase();
        encodedVocabularyLabel = encodeURIComponent(vocabularyLabel);
        encodedRelationLabel = encodeURIComponent(relationLabel);
        timestamp = D.timestamp();
        $.each(D.relations, function(label, r){
            if(r.vocabularyLabel === vocabularyLabel){
                vocabularyId = r.vocabularyId;
                return false;
            }
        });
        if(!vocabularyId){
            vocabularyId = 'http://desanti.org/vocabularies/'+timestamp;
            D.request("addVocabulary.txt",
                {
                    vocabLabel: encodedVocabularyLabel,
                    vocabId: vocabularyId,
                    user: D.userName,
                    timestamp: timestamp
                },makeRelation, true);
        }else{ makeRelation(); }
    }

    autocompleteRelations = function(){
        var autocompleteList = [];

        $.each(D.relations, function(item){
            autocompleteList.push(item);
        });
        D.autocomplete(autocompleteList, 
            domInput,
            function(item){
                if(!D.relations[item]){
                    addRelation(item);
                }
            }, modalWindow);
    }

    D.initRelations = function(inputId, fn, win){
        modalWindow = win;
        domInput = document.getElementById(inputId);
        D.request("get_relations.txt",{},function(results){
            $.each(results, function(i,r){
                var relationLabel = decodeURIComponent(r.rLabel.value),
                    vocabularyLabel = decodeURIComponent(r.vLabel.value);
                D.relations[vocabularyLabel+':'+relationLabel]={
                    id: r.r.value,
                    label: relationLabel,
                    vocabularyId: r.v.value,
                    vocabularyLabel: vocabularyLabel
                };
            });
            autocompleteRelations();
            if(fn) fn();
        });
    }
})(DESANTI);
